# -*- python -*-
# ex: set syntax=python:

# This is a sample buildmaster config file. It must be installed as
# 'master.cfg' in your buildmaster's base directory (although the filename
# can be changed with the --basedir option to 'mktap buildbot master').

# It has one job: define a dictionary named BuildmasterConfig. This
# dictionary has a variety of keys to control different aspects of the
# buildmaster. They are documented in docs/config.xhtml .


# This is the dictionary that the buildmaster pays attention to. We also use
# a shorter alias to save typing.
c = BuildmasterConfig = {}

####### BUILDSLAVES

# the 'slaves' list defines the set of allowable buildslaves. Each element is
# a BuildSlave object, which is created with bot-name, bot-password.  These
# correspond to values given to the buildslave's mktap invocation.
from buildbot.buildslave import BuildSlave
c['slaves'] = [BuildSlave("bot-dmz2", "passwd"),
               BuildSlave("bot-grok2", "passwd"), 
               BuildSlave("bot-grok2-clang", "passwd"), 
               BuildSlave("bot-fs2", "passwd"), 
               BuildSlave("bot-fs", "passwd"), 
               BuildSlave("bot-dt-arm-cx", "passwd"), 
              ]

# to limit to two concurrent builds on a slave, use
#  c['slaves'] = [BuildSlave("bot1name", "bot1passwd", max_builds=2)]


# 'slavePortnum' defines the TCP port to listen on. This must match the value
# configured into the buildslaves (with their --master option)

c['slavePortnum'] = 9989

####### CHANGESOURCES

# the 'change_source' setting tells the buildmaster how it should find out
# about source code changes. Any class which implements IChangeSource can be
# put here: there are several in buildbot/changes/*.py to choose from.

from buildbot.changes.pb import PBChangeSource
c['change_source'] = PBChangeSource()

# For example, if you had CVSToys installed on your repository, and your
# CVSROOT/freshcfg file had an entry like this:
#pb = ConfigurationSet([
#    (None, None, None, PBService(userpass=('foo', 'bar'), port=4519)),
#    ])

# then you could use the following buildmaster Change Source to subscribe to
# the FreshCVS daemon and be notified on every commit:
#
#from buildbot.changes.freshcvs import FreshCVSSource
#fc_source = FreshCVSSource("cvs.example.com", 4519, "foo", "bar")
#c['change_source'] = fc_source

# or, use a PBChangeSource, and then have your repository's commit script run
# 'buildbot sendchange', or use contrib/svn_buildbot.py, or
# contrib/arch_buildbot.py :
#
#from buildbot.changes.pb import PBChangeSource
#c['change_source'] = PBChangeSource()

# If you wat to use SVNPoller, it might look something like
#  # Where to get source code changes
# from buildbot.changes.svnpoller import SVNPoller
# source_code_svn_url='https://svn.myproject.org/bluejay/trunk'
# svn_poller = SVNPoller(
#                    svnurl=source_code_svn_url,
#                    pollinterval=60*60, # seconds
#                    histmax=10,
#                    svnbin='/usr/bin/svn',
## )
# c['sources'] = [ svn_poller ]

####### SCHEDULERS

## configure the Schedulers

from buildbot.scheduler import Scheduler
c['schedulers'] = []
c['schedulers'].append(Scheduler(name="all", branch="master",
                                 treeStableTimer=2*60,
                                 builderNames=["dmz2-F13-64",
                                               "grok2-F13-32",
                                               "grok2-F13-32-clang",
                                               "fs2-FC8-32",
                                               "fs-FC5-32",
                                               "dt-arm-cx",
                                              ]))


####### BUILDERS

# the 'builders' list defines the Builders. Each one is configured with a
# dictionary, using the following keys:
#  name (required): the name used to describe this builder
#  slavename (required): which slave to use (must appear in c['bots'])
#  builddir (required): which subdirectory to run the builder in
#  factory (required): a BuildFactory to define how the build is run
#  periodicBuildTime (optional): if set, force a build every N seconds

# buildbot/process/factory.py provides several BuildFactory classes you can
# start with, which implement build processes for common targets (GNU
# autoconf projects, CPAN perl modules, etc). The factory.BuildFactory is the
# base class, and is configured with a series of BuildSteps. When the build
# is run, the appropriate buildslave is told to execute each Step in turn.

# the first BuildStep is typically responsible for obtaining a copy of the
# sources. There are source-obtaining Steps in buildbot/steps/source.py for
# CVS, SVN, and others.

#cvsroot = ":pserver:anonymous@cvs.sourceforge.net:/cvsroot/buildbot"
#cvsmodule = "buildbot"

from buildbot import locks

# Cannot run two 'scons check' steps concurrently because
# then there will be port conflicts.
check_lock = locks.MasterLock("checks")

gitrepourl = "git://github.com/greearb/xorp.ct.git"

from buildbot.process import factory
from buildbot.steps.source import Git
from buildbot.steps.shell import Compile
from buildbot.steps.shell import ShellCommand
from buildbot.steps.python_twisted import Trial

class MyShellCommand(ShellCommand):
  def createSummary(self, log):
    failures = []
    passed = []
    self.pcount = 0
    self.fcount = 0
    for line in log.readlines():
        if "Tests Failed" in line:
            self.fcount += 1
            failures.append(line)
        elif "Test Failed" in line:
            self.fcount += 1
            failures.append(line)
        elif "Tests Succeeded" in line:
            self.pcount += 1
            passed.append(line)
        elif "Test Passed" in line:
            self.pcount += 1
            passed.append(line)
        elif "Test Succeeded" in line:
            self.pcount += 1
            passed.append(line)

    failures.append("Total test failures: %i" % self.fcount)
    passed.append("Total test successes: %i" % self.pcount)
    self.addCompleteLog("failures: %i" % self.fcount, "".join(failures))
    self.addCompleteLog("passed: %i" % self.pcount, "".join(passed))

# B with j4...good for systems who have lots of CPUs and
# are otherwise not so busy.
f1 = factory.BuildFactory()
f1.addStep(Git(repourl=gitrepourl, mode="copy"))
#f1.addStep(Compile(command=["python", "./setup.py", "build"]))
f1.addStep(ShellCommand(command=["rm", "-fr", "obj"], workdir="build/xorp", descriptionDone="clean"))
f1.addStep(ShellCommand(command=["scons", "-j4"], workdir="build/xorp", description="compiling -j4", descriptionDone="compile -j4"))
f1.addStep(MyShellCommand(command=["scons", "check"], workdir="build/xorp", description="checking", descriptionDone="check"))
#f1.addStep(Trial(testChanges=True, testpath="."))

# Go easy on machine...build with single compile process (-j1 effectively)
f2 = factory.BuildFactory()
f2.addStep(Git(repourl=gitrepourl, mode="copy"))
f2.addStep(ShellCommand(command=["rm", "-fr", "obj"], workdir="build/xorp", descriptionDone="clean"))
f2.addStep(ShellCommand(command=["scons"], workdir="build/xorp", description="compiling", descriptionDone="compile"))
f2.addStep(MyShellCommand(command=["scons", "check"], workdir="build/xorp", description="checking", descriptionDone="check"))

# Go easy on machine...build with single compile process (-j1 effectively)
# Lock 'check' step so we can run 'concurrent' slaves
f2_lck = factory.BuildFactory()
f2_lck.addStep(Git(repourl=gitrepourl, mode="copy"))
f2_lck.addStep(ShellCommand(command=["rm", "-fr", "obj"], workdir="build/xorp", descriptionDone="clean"))
f2_lck.addStep(ShellCommand(command=["scons"], workdir="build/xorp", description="compiling", descriptionDone="compile"))
f2_lck.addStep(MyShellCommand(command=["scons", "check"], workdir="build/xorp",
                              description="checking-lock", descriptionDone="check-lock",
                              locks=[check_lock.access('exclusive')]))



# Cross-compile to arm target
arm_cx = factory.BuildFactory()
arm_cx.addStep(Git(repourl=gitrepourl, mode="copy"))
arm_cx.addStep(ShellCommand(command=["rm", "-fr", "obj"], workdir="build/xorp", descriptionDone="clean"))
arm_cx.addStep(ShellCommand(command=["scons",
                                     "build=arm-unknown-linux-gnu",
                                     "CC=/home/greearb/x-tools/arm-unknown-linux-gnueabi/bin/arm-unknown-linux-gnueabi-gcc",
				     "CXX=/home/greearb/x-tools/arm-unknown-linux-gnueabi/bin/arm-unknown-linux-gnueabi-g++",
				     "CFLAGS=-I/home/greearb/x-tools/arm-unknown-linux-gnueabi/local/include",
                                     "CXXFLAGS=-I/home/greearb/x-tools/arm-unknown-linux-gnueabi/local/include",
				     "LINKFLAGS=-L/home/greearb/x-tools/arm-unknown-linux-gnueabi/local/lib",
				     "-j4",
                                    ],
                            workdir="build/xorp", description="compiling", descriptionDone="compile-arm-cx"))

# Build with clang/llvm
# Exclusive lock with f2_lck for 'check' step.
fclang = factory.BuildFactory()
fclang.addStep(Git(repourl=gitrepourl, mode="copy"))
fclang.addStep(ShellCommand(command=["rm", "-fr", "obj"], workdir="build/xorp", descriptionDone="clean"))
fclang.addStep(ShellCommand(command=["scons", "CC=clang", "CXX=clang++"], workdir="build/xorp", description="compiling", descriptionDone="compile-clang"))
fclang.addStep(MyShellCommand(command=["scons", "CC=clang", "CXX=clang++", "check"],
                              workdir="build/xorp", description="checking-lock", descriptionDone="check-lock",
                              locks=[check_lock.access('exclusive')]))



b1 = {'name': "dmz2-F13-64",
      'slavename': "bot-dmz2",
      'builddir': "full",
      'factory': f1,
      }
b2 = {'name': "grok2-F13-32",
      'slavename': "bot-grok2",
      'builddir': "full2",
      'factory': f2_lck,
      }
b3 = {'name': "fs2-FC8-32",
      'slavename': "bot-fs2",
      'builddir': "full3",
      'factory': f2,
      }
b4 = {'name': "fs-FC5-32",
      'slavename': "bot-fs",
      'builddir': "full4",
      'factory': f2,
      }
b5 = {'name': "grok2-F13-32-clang",
      'slavename': "bot-grok2-clang",
      'builddir': "full5",
      'factory': fclang,
      }
b6 = {'name': "dt-arm-cx",
      'slavename': "bot-dt-arm-cx",
      'builddir': "arm-cx",
      'factory': arm_cx,
      }


c['builders'] = [b1, b2, b3, b4, b5, b6]


####### STATUS TARGETS

# 'status' is a list of Status Targets. The results of each build will be
# pushed to these targets. buildbot/status/*.py has a variety to choose from,
# including web pages, email senders, and IRC bots.

c['status'] = []

# Use allowForce=True (boolean, not a string. ie: not 'True') to allow
# Forcing Builds in the Web User Interface. The default is False.
from buildbot.status import html
c['status'].append(html.WebStatus(http_port=8010,allowForce=True))

#from buildbot.status import html
#c['status'].append(html.WebStatus(http_port=8010))

# from buildbot.status import mail
# c['status'].append(mail.MailNotifier(fromaddr="buildbot@localhost",
#                                      extraRecipients=["builds@example.com"],
#                                      sendToInterestedUsers=False))
#
# from buildbot.status import words
# c['status'].append(words.IRC(host="irc.example.com", nick="bb",
#                              channels=["#example"]))
#
# from buildbot.status import client
# c['status'].append(client.PBListener(9988))


####### DEBUGGING OPTIONS

# if you set 'debugPassword', then you can connect to the buildmaster with
# the diagnostic tool in contrib/debugclient.py . From this tool, you can
# manually force builds and inject changes, which may be useful for testing
# your buildmaster without actually committing changes to your repository (or
# before you have a functioning 'sources' set up). The debug tool uses the
# same port number as the slaves do: 'slavePortnum'.

#c['debugPassword'] = "debugpassword"

# if you set 'manhole', you can ssh into the buildmaster and get an
# interactive python shell, which may be useful for debugging buildbot
# internals. It is probably only useful for buildbot developers. You can also
# use an authorized_keys file, or plain telnet.
#from buildbot import manhole
#c['manhole'] = manhole.PasswordManhole("tcp:9999:interface=127.0.0.1",
#                                       "admin", "password")


####### PROJECT IDENTITY

# the 'projectName' string will be used to describe the project that this
# buildbot is working on. For example, it is used as the title of the
# waterfall HTML page. The 'projectURL' string will be used to provide a link
# from buildbot HTML pages to your project's home page.

c['projectName'] = "xorp.ct"
c['projectURL'] = "http://www.candelatech.com/xorp.ct/"

# the 'buildbotURL' string should point to the location where the buildbot's
# internal web server (usually the html.Waterfall page) is visible. This
# typically uses the port number set in the Waterfall 'status' entry, but
# with an externally-visible host name which the buildbot cannot figure out
# without some help.

c['buildbotURL'] = "http://dmz2.candelatech.com:8010/"
